package net.zjitc.filter;




import com.sun.istack.internal.logging.Logger;
import org.apache.logging.log4j.core.layout.LoggerFields;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebFault;
import java.io.IOException;

/**
 * Created by Administrator on 2017/11/13.
 */
@WebFilter(filterName ="MyFilter",urlPatterns = "/aServlet")
public class MyFilter implements Filter{
    private Logger logger = Logger.getLogger(LoggerFields.class);
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setContentType("text/html;charset=utf-8");
        Object admin = request.getAttribute("admin");
        if(admin==null||"".equals(admin)){
            logger.info("logger:   "+request.getServletPath());
            System.out.println("logger:   "+request.getServletPath());
            request.setAttribute("msg","请先登录");
            request.getRequestDispatcher("/aServlet").forward(request,response);
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    public void destroy() {

    }
}
